#人脸识别

# 不能颠倒
from django.conf import settings
import face_recognition
import os
def regonize(now_checkPath:str):
    base_dir=os.path.join(settings.MEDIA_ROOT,'face')
    baselist=os.listdir(base_dir)
    try:
        for person in baselist:
            base_img = face_recognition.load_image_file(os.path.join(base_dir,person))
            unknown_image = face_recognition.load_image_file(now_checkPath)
            base_img_encoding = face_recognition.face_encodings(base_img, num_jitters=3)[0]
            unknown_encoding = face_recognition.face_encodings(unknown_image, num_jitters=3)[0]
            results = face_recognition.compare_faces([base_img_encoding], unknown_encoding)
            return results[0],'face/'+person
    except Exception as e:
        print(e)
        return False,''


import time
TOPIC_LIST=["regonize","access","noneone"]
from paho.mqtt import client as mqt_client

class MyMqttClient(object):
    def __init__(self,broker='120.76.201.116'):
        self.client_id=f'django-recv-{time.time()}'
        self.client=mqt_client.Client(client_id=self.client_id)
        self.client.connect(broker)
        self.client.on_message=self.on_message
    def on_message(self,client, userdata, msg):
        #print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
        topic=msg.topic
        data=msg.payload.decode()
        print(topic,data)
    def order(self):
        for top in TOPIC_LIST:
            self.client.subscribe(top)
    def run(self):
        self.order()
        def on_disconnect(client,c1,c2):
            self.client.loop_stop()
            exit(0)
        self.client.on_disconnect=on_disconnect
        self.client.loop_start()

